app.controller('PrcieModalCtrl', function ($scope, $modalInstance, $modal, toaster, param, ajax) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.submitting = false;
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	//获取组织列表
	$scope.loadOrgList = function () {
		ajax.call(new Req.GET({
			resource: 'organization/2/list'
		})).then(function (result) {
			$scope.orglist = result.result;
		}, function () { });
	};
	//获取定价模版列表
	$scope.loadPriceList = function () {
		ajax.call(new Req.GET({
			resource: 'price/rt/list'
		})).then(function (result) {
			$scope.pricelist = result.result;
			//设置默认值
			if ($scope.param.data.price != null) {
				var id = $scope.param.data.price.id;
				var sel = 0;
				for (var i = 0; i < $scope.pricelist.length; i++) {
					sel++;
					if ($scope.pricelist[i].id == id) {
						break;
					}
				}
				$scope.pricelist.selected = $scope.pricelist[sel - 1];
			}
		}, function () { });
	};

	//获取城市列表
	$scope.loadCityList = function () {
		var promise = ajax.call(new Req.GET({
			resource: 'city/rt/list'
		}));
		promise.then(function (result) {
			$scope.city1list = result.result;
			$scope.city2list = angular.copy(result.result);
		}, function () { });
	};

	if ($scope.param.type == 'insert') {
		$scope.loadOrgList();
		$scope.loadCityList();
	}

	if ($scope.param.type == "update") {
		ajax.call(new Req.GET({
			resource: 'organization/' + $scope.param.data.organizationId + '/id'
		})).then(function (result) {
			$scope.org = result.result;
		}, function () { });
	}
	$scope.loadPriceList();


	$scope.addCity = function () {
		$scope.type = "insert";
		var modalInstance = $modal.open({
			templateUrl: 'template/rent/city/writeModal.html?v=680266797e',
			controller: 'RtCityModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": $scope.type
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			inserter.fields = param.data;
			inserter.resource = 'city/rt/insert';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					$scope.city = result.result;
					$scope.loadCityList();
					toaster.pop('success', '成功', '新增信息成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '新增城市失败。');
			});
		});
	};

	$scope.addPrice = function () {
		$scope.type = "insert";
		var modalInstance = $modal.open({
			templateUrl: 'template/rent/price/writeModal.html?v=abaa4c3cb2',
			controller: 'PriceModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": $scope.type
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			inserter.fields = param.data;
			inserter.resource = 'price/rt/insert';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					$scope.price = result.resut;
					$scope.loadPriceList();
					toaster.pop('success', '成功', '新增信息成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '新增定价模板失败。');
			});
		});
	};

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if ($scope.param.type == 'insert') {
				if ($scope.city1list.selected == undefined) {
					toaster.pop('warning', '提示', '请选择起始城市！');
					return;
				}
				if ($scope.city2list.selected == undefined) {
					toaster.pop('warning', '提示', '请选择目的城市！');
					return;
				}
				if ($scope.pricelist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择价格模版！');
					return;
				}
				if ($scope.orglist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择所属单位！');
					return;
				}
				if ($scope.city1list.selected.code == $scope.city2list.selected.code) {
					toaster.pop('warning', '提示', '起始城市和目的城市不可相同！');
					return;
				}
				$scope.param.data.city1 = $scope.city1list.selected;
				$scope.param.data.city2 = $scope.city2list.selected;
				$scope.param.data.price = $scope.pricelist.selected;
				$scope.param.data.organizationId = $scope.orglist.selected.id;
				$modalInstance.close($scope.param);
			} else if ($scope.param.type == 'update') {
				if ($scope.pricelist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择价格模版！');
					return;
				}
				$scope.param.data.price = $scope.pricelist.selected;
				$modalInstance.close($scope.param);
			}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};
});